package org.phalanx.gwt.client.graphics.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MapTileSet {
	private HashMap<String,MapTile> mapTileTable;
	private String authorName;
	private String email;
	private String country;
	private String tileSetName;
	private String tileSetArchiveFile;
	private String tileSetCanonicalFilePath;
	
	public MapTileSet (){
		mapTileTable = new HashMap<String,MapTile>();
	}
	
	public void setMapTile(String referenceName, MapTile tile){
		if (referenceName != null && tile != null){
			mapTileTable.put(referenceName.toLowerCase(), tile);
		}
	}
	
	public MapTile getMapTileByName(String ref){
		MapTile requestedTile = null;
		String qRef = ref.toLowerCase();
		
		if (mapTileTable.containsKey(qRef)){
			requestedTile = mapTileTable.get(qRef);
		}
		
		return requestedTile;
	}
	
	public void removeMapTile(String ref){
		if (mapTileTable.containsKey(ref.toLowerCase())){
			mapTileTable.remove(ref);
		}
	}
	
	public void clearMapTileSet(){
		mapTileTable.clear();
	}
	
	public HashMap<String,MapTile> getMapTileSetTable(){
		return mapTileTable;
	}
	
	public void setAuthorName(String author){
		this.authorName = author;
	}
	
	public String getAuthorName(){
		return this.authorName;
	}
	
	public void setTileSetName(String tileSetName){
		this.tileSetName = tileSetName;
	}
	
	public String getTileSetName(){
		return this.tileSetName;
	}
	
	public void setEmailAddress(String emailAddr){
		this.email = emailAddr; 
	}
	
	public String getEmailAddress(){
		return this.email;
	}
	
	public void setCountry(String cy){
		this.country = cy;
	}
	
	public String getCountry(){
		return this.country;
	}

	public String getTileSetArchiveFile() {
		return tileSetArchiveFile;
	}

	public void setTileSetArchiveFile(String tileSetArchiveFile) {
		this.tileSetArchiveFile = tileSetArchiveFile;
	}
	
	public String getCanonicalFilePath(){
		return tileSetCanonicalFilePath;
	}
	
	public void setCanonicalFilePath(String path){
		tileSetCanonicalFilePath = path;
	}
	
	public boolean isUsingDefaultTileSet(){
		return getTileSetArchiveFile() == null;
	}
	
	public String getTerrainRefNameFromTile(MapTile t){
		String refName = null;
		boolean isFound = false;

		if (t != null){
			Set<String> e = mapTileTable.keySet();
			Iterator<String> i = e.iterator();
			
			while(i.hasNext() && !isFound){
				String tempName = i.next();
				String referenceFile = mapTileTable.get(tempName).getImageReference();

				if (referenceFile.equals(t.getImageReference())){
					refName = tempName;
					isFound = true;
				}
			}
		}
		return refName;
	}
}
